package com.uni.dao;

import com.uni.entity.Commodity;
import com.uni.entity.CommodityRecord;
import com.uni.entity.CommodityType;
import com.uni.entity.User;
import com.uni.service.Service;
import com.uni.utils.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * --------------project-------------
 *
 * @Author Unirithe  - Mr.Chao
 * - CSDN:
 * https://blog.csdn.net/Unirithe
 * - GitHub:
 * https://github.com/unirithe
 * @create 2021/12/18 12:18
 * ------------------------------------------
 */
public class UserDaoImpl implements UserDao{
    @Override
    public User queryUserById(int id) {
        Connection connection = DruidUtil.getConnection();
        String sql = "select * from user where id = ?";
        User user = null;
        try{
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1, id);
            ResultSet resultSet = pstmt.executeQuery();
            while(resultSet.next())
                user = new User(resultSet.getInt(1), resultSet.getString(2),
                        resultSet.getString(3), resultSet.getString(4));
            DruidUtil.release(connection, pstmt, resultSet);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    @Override
    public User queryUserByName(String name) {
        Connection connection = DruidUtil.getConnection();
        String sql = "select * from user where name = ?";
        User user = null;
        try{
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, name);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){
                user = new User(rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4));
                return user;
            }
            DruidUtil.release(connection, pstmt, rs);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public User queryByPhone(String phone) {
        Connection connection = DruidUtil.getConnection();
        String sql = "select * from user where phone = ?";
        User user = null;
        try{
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, phone);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){
                user = new User(rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4));
            }
            DruidUtil.release(connection, pstmt, rs);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    @Override
    public boolean insertUser(User user) {
        Connection connection = DruidUtil.getConnection();
        String sql = "insert into user (name, password, phone) values (?, ?, ?)";
        try {
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, user.getName());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getPhone());
            pstmt.execute();
            DruidUtil.release(connection, pstmt, null);
            return true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return false;
        }
    }

    @Override
    public User checkUser(String name, String password) {
        Connection connection = DruidUtil.getConnection();
        String sql = "select * from user where name = ? and password = ?";
        User user = null;
        try {
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, password);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){
                user = new User(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4));
            }
            DruidUtil.release(connection, pstmt, rs);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    @Override
    public boolean updateUSer(User user) {
        Connection connection = DruidUtil.getConnection();
        String sql = "update user set phone = ? where id = ?";
        int result = 0;
        try{
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, user.getPhone());
            pstmt.setInt(2, user.getId());
            result = pstmt.executeUpdate();
            DruidUtil.release(connection, pstmt, null);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result == 1;
    }

    @Override
    public List<User> queryAllUser() {
        List<User> list = new ArrayList<>();
        Connection connection = DruidUtil.getConnection();
        String sql = "select * from user";
        try{
            PreparedStatement pstmt = connection.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next())
                list.add(new User(rs.getInt(1), rs.getString(2),
                        rs.getString(3), rs.getString(4)));
            DruidUtil.release(connection, pstmt, rs);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public List<CommodityRecord> queryHistory(User user) {
        List<CommodityRecord> list = new ArrayList<>();
        Connection connection = DruidUtil.getConnection();
        String sql = "select * from user_action where uid = ?";
        try{
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1, user.getId());
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){
                list.add(new CommodityRecord(
                        rs.getInt(1),
                        user.clone(),
                        Service.commodity.query(rs.getInt(3)),
                                rs.getTimestamp(4)));
            }
            DruidUtil.release(connection, pstmt, rs);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean deleteUserById(int id) {
        boolean ok = false;
        Connection connection = DruidUtil.getConnection();
        String sql = "delete from user where id = ?";
        try{
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1, id);
            ok = pstmt.execute();
            DruidUtil.release(connection, pstmt, null);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return ok;
    }

    @Override
    public boolean deleteHistoryById(int id) {
        boolean ok = false;
        Connection connection = DruidUtil.getConnection();

        String sql = "delete from user_action where uid = ?";
        try{
            PreparedStatement pstmt = connection.prepareStatement(sql);
            pstmt.setInt(1, id);
            ok = pstmt.execute();
            DruidUtil.release(connection, pstmt, null);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return ok;
    }
}
